﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

using SPBusiness.ExtensionMethods;


namespace SPBusiness
{
   public sealed class Folder
    {
       /// <summary>
       /// Folder class has only Static Methods, no constructor needed.
       /// </summary>
       private Folder() { }

        public static void PublishAndApproveFilesRecursive(SPFolder pSPFolder)
        {
            try
            {

                if (pSPFolder.Files != null)
                {
                    foreach (SPFile vSPFile in pSPFolder.Files)
                    {
                        if (vSPFile.Item != null)
                        {
                            File.ApproveElevated(vSPFile.Web.Site.Url, vSPFile.Web.ServerRelativeUrl, vSPFile.ServerRelativeUrl);
                        }

                    }
                }

                if (pSPFolder.SubFolders != null)
                {
                    foreach (SPFolder vSPFolder in pSPFolder.SubFolders)
                    {
                        if (vSPFolder.Name != "Forms")
                        {
                            PublishAndApproveFilesRecursive(vSPFolder);
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Folder).Namespace, typeof(Folder).Name, "PublishAndApproveFilesRecursive", ex);
                throw new Exception(ex.Message + "\r\n" + pSPFolder.Url, ex);
            }
        }

        public static void CheckInFilesRecursive(SPFolder pSPFolder, SPCheckinType pSPCheckinType)
        {
            try
            {

                foreach (SPFile vSPFile in pSPFolder.Files)
                {
                    if (vSPFile.Item != null)
                    {
                        File.CheckInElevated(vSPFile.Web.Site.Url, vSPFile.Web.ServerRelativeUrl, vSPFile.ServerRelativeUrl, pSPCheckinType);
                    }

                }

                foreach (SPFolder vSPFolder in pSPFolder.SubFolders)
                {
                    if (vSPFolder.Name != "Forms")
                    {
                        CheckInFilesRecursive(vSPFolder, pSPCheckinType);
                    }
                }

            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Folder).Namespace, typeof(Folder).Name, "CheckInFilesRecursive", ex);
                throw;
            }
        }

        public static void DeleteListItemByName(SPFolder pSPFolder, string pName, Boolean pRecursive)
        {
            try
            {
                SPList vSPList = pSPFolder.ParentWeb.Lists.TryGetListById(pSPFolder.ParentListId);

                if (vSPList != null)
                {
                    SPQuery vSPQuery = new SPQuery();

                    vSPQuery.Folder = pSPFolder;

                    if (pRecursive)
                    {
                        vSPQuery.ViewAttributes = "Scope=\"RecursiveAll\"";
                    }

                    vSPQuery.Query = string.Concat(
                  "<Where>",
                        "<Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>" + pName + "</Value></Eq>",
                  "</Where>");

                    SPListItemCollection vSPListItemCollection = vSPList.GetItems(vSPQuery);

                    if (vSPListItemCollection.Count > 0)
                    {
                        IEnumerable<int> vInts = from vSPListItemLinq in vSPListItemCollection.OfType<SPListItem>()
                                                 select vSPListItemLinq.ID;

                        foreach (int vID in vInts.ToList())
                        {
                            vSPList.GetItemById(vID).Delete();

                        }
                    }
                }

            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Folder).Namespace, typeof(Folder).Name, "DeleteListItemByName", ex);
                throw new Exception(ex.Message + "\r\n" + pSPFolder.Url, ex);
            }
        }
    }
}
         